$(document).ready(function () {
    init(callback, 1);
});


function init(callback, current) {
    $("#thislist").html("");
    $.ajax({
        type: "get",
        url: urlcore + "/api/questionnaire/selectAll?current=" + current+"&type="+$("#qnType").val(),
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            if (data.success == true) {
                callback(data.data.records);
                $('#thiscount').text(data.data.total);
                $("#pager").pager({
                    pagenumber: current,
                    pagecount: data.data.pages,
                    totalcount: data.data.total,
                    buttonClickCallback: PageClick
                });
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("查询失败！");
        }
    });

}


/*//回调函数
PageClick = function (pageclickednumber) {
    var type = getvl("goodsType") == "" ? 0 : getvl("goodsType");
    init(type, callback, pageclickednumber);
}*/
//回调函数
PageClick = function (pageclickednumber) {
    init(callback, pageclickednumber);
}

var callback = function (data) {
    var html = "";
    //问卷类型
    for (var i = 0; i < data.length; i++) {
        id = data[i].id;
        var qnType="";
        if(data[i].type==1){
            qnType="试用众测问卷";
        }else if(data[i].type==2){
            qnType="有奖调查问卷";
        }
        var addtime = data[i].addtime != null ? new Date(data[i].addtime).pattern("yyyy-MM-dd hh:mm:ss") : "";
        html += "  <tr id=\"" + id + "\">\n" +
            "  \t\t<td>" + (i + 1) + "</td>\n" +
            "  \t\t<td>" + data[i].title + "</td>\n" +
            "  \t\t<td>" + qnType + "</td>\n" +
            "  \t\t<td>" + addtime + "</td>\n" +
            "  \t\t<td>\n" +
            "   \t\t\t<a class=\"btn btn-danger btn-xs\" href=\"javascript:;\" onclick=\"doDelete(" + id + ")\"> 删除</a>\n" +
            "   \t\t\t<a class=\"btn btn-info btn-xs\"  href=\"javascript:;\" onclick=\"edit(" + id + ")\"> 详细</a>\n";
        if (data[i].type == 2) {
            html += "<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"openPublishModal(" + id + ",\'" + data[i].title + "\'"+ ",\'" + data[i].expireTime + "\')\" data-toggle=\"modal\" data-target=\"#publishModal\"> 发布</a>\n";
        }
        html += "  \t\t</td>\n" +
            "  \t</tr>";
    }
    $("#thislist").append(html);
}


//刪除
function doDelete(id) {
    if (confirm("删除后将不可恢复")) {
        $.ajax({
            type: "GET",
            url: urlcore + "/api/questionnaire/deletequestionnaire?id=" + id,
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: {},
            success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
                if (data.success == true) {
                    location.reload();
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("删除失败");
            }
        });
    }
}

//修改
function edit(id) {
    location.href = "querstion_edit.html?id=" + id;
}

//问卷问题内容修改保存
function subQues(id) {
    $.ajax({
        type: "POST",
        url: urlcore + "/api/questionnaire/update",
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: JSON.stringify({
            title: $("#title").val(),
            content: $("#content").val(),
            id: id
        }),
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            if (data.success == true) {
                //location.reload();
                alert("保存成功");
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("保存失败");
        }
    });
}


//单个问卷选项修改
function subOption(id) {

    $.ajax({
        type: "POST",
        url: urlcore + "/api/questionnaire/opsUpdate",
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: JSON.stringify({
            options: $("#ops" + id + "").val(),
            content: $("#cot" + id + "").val(),
            id: id
        }),
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            if (data.success == true) {
                //location.reload();
                alert("保存成功");
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("保存失败");
        }
    });
}


//搜索
function searchuser() {
    var type = $('#goods_type option:selected').val();//选中的值
    init(type, callback, 1);
}


//打开发布模态框，填充消息数据
function openPublishModal(row_id, title,expireTime) {
    //要先设置bootstrap-table中的uniqueId: "id",得到该行原始数据
    // var row = $table.bootstrapTable('getRowByUniqueId', row_id);
    $("#publishModal").attr("data-id", row_id);
    $("#publishModal").attr("expireTime", expireTime);
    $("#modalTitle").text(title);
    // $("#modalContent").text(row.content);
}

//发布问卷
function publish() {
    //id
    var quId = $("#publishModal").attr("data-id");
    var expireTime = $("#publishModal").attr("expireTime");
    if (quId == undefined || quId == "") {
        return;
        window.location.reload();
    }
    var sex = $("input[name='sexRadio']:checked").val();
    var memberType = $("#modalMemberType").val();
    var minAge = $("#modalMinAge").val();
    var maxAge = $("#modalMaxAge").val();

    if (minAge != "" && maxAge != "") {
        minAge = parseInt(minAge);
        maxAge = parseInt(maxAge);
        if (minAge >= maxAge) {
            alert("筛选最大年龄必须不小于最小年龄");
            return;
        }
    }

    var muser = new Object();
    if (sex != "") {
        muser.sex = sex;
    }
    if (memberType != "") {
        muser.memberType = memberType;
    }
    var postData = {
        "minAge": minAge,
        "maxAge": maxAge,
        "muser": muser
    }
    $.ajax({
        type: "POST",
        url: urlcore + "/api/userQuestionnaire/publishMsg4PartUsers?quId=" + quId+"&expireTime="+expireTime,
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(postData),
        success: function (data) {
            console.log("cbdata", data);
            if(data.success){
                alert("发布成功");
                window.location.reload();
            }
        },
        error: function () {
            console.log("error");
        }
    });
}


function changeQnType(type) {
    init(callback, 1);
}

